package com.lening.controller;

import com.lening.pojo.TbBrand;
import com.lening.sellergoods.service.BrandService;
import entity.PageResult;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    @RequestMapping("getBrandList")
    public List<Map> getBrandList(){
        return brandService.selectOptionList();
    }

    @RequestMapping("brandFindAll")
    public List<TbBrand> BrandFindAll(){
        List<TbBrand> all = brandService.findAll();
        return all;
    }

    @RequestMapping("brandFindPage")
    public PageResult brandFindPage(Integer pageNum, Integer pageSize){
        PageResult page = brandService.findPage(pageNum, pageSize);
        return page;
    }

    @RequestMapping("findOne")
    public TbBrand findOne(Long id){
        TbBrand one = brandService.findOne(id);
        return one;
    }

    @RequestMapping("saveBrand")
    public Result saveBrand(@RequestBody TbBrand entity){
        System.out.println(entity.toString());
        int res = 0;
        try {
            if(entity!=null){
                if(entity.getId()!=null){
                     //修改
                     brandService.update(entity);
                }else{
                    //添加
                     brandService.add(entity);
                }
            }
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res>0?new Result(true,"保存成功"):new Result(false,"保存失败");
    }

    @RequestMapping("deleteBrand")
    public Result deleteBrand(@RequestBody Long[] ids){
        int res = 0;
        try {
            brandService.delete(ids);
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
       return res>0?new Result(true,"删除成功"):new Result(false,"删除失败");
    }

    /**
     * 条件分页查询
     * @param searchEntity
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("search")
    public PageResult search(Integer pageNum, Integer pageSize, @RequestBody TbBrand searchEntity){
        PageResult page = brandService.search(pageNum, pageSize,searchEntity);
        return page;
    }

}
